package cn.org.timespace.easyframe.service.permission.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import cn.org.timespace.easyframe.action.EfActionConstant;
import cn.org.timespace.easyframe.action.permission.EFUserModel;
import cn.org.timespace.easyframe.dao.permission.EfUserDao;
import cn.org.timespace.easyframe.dao.permission.EfUserRoleDao;
import cn.org.timespace.easyframe.model.permission.EfMenu;
import cn.org.timespace.easyframe.model.permission.EfUser;
import cn.org.timespace.easyframe.model.permission.EfUserLoginLog;
import cn.org.timespace.easyframe.model.permission.EfUserRole;
import cn.org.timespace.easyframe.model.permission.SystemMessage;
import cn.org.timespace.easyframe.service.permission.EfMenuService;
import cn.org.timespace.easyframe.service.permission.EfMenuShortcutService;
import cn.org.timespace.easyframe.service.permission.EfUserLoginLogService;
import cn.org.timespace.easyframe.service.permission.EfUserService;
import cn.org.timespace.easyframe.service.permission.SystemMessageService;
import cn.org.timespace.easyframe.util.generator.IdentityGenerator;
import cn.org.timespace.easyframe.util.web.Security;

/**
 * <p>
 * 登录认证接口实现类
 * </p>
 * 
 * @version 1.0, 11/01/18
 * @since 1.0
 * @author wubo
 */

public class EfUserServiceImpl implements EfUserService {

	private static Log log = LogFactory.getLog(EfUserServiceImpl.class);
	
	private EfUserDao efUserDao;
	
	//用户
	private EfUserService efUserService;
	//用户登录日志
	private EfUserLoginLogService efUserLoginLogService;
	//菜单
	private EfMenuService efMenuService;
	//快捷菜单
	private EfMenuShortcutService efMenuShortcutService;
	//系统公告
	private SystemMessageService systemMessageService;
	
	private EfUserRoleDao efUserRoleDao;//用户角色

	public EfUser authentication(EfUser efUser) {
		return efUserDao.authentication(efUser);
	}

	public EfUserDao getEfUserDao() {
		return efUserDao;
	}

	public void setEfUserDao(EfUserDao efUserDao) {
		this.efUserDao = efUserDao;
	}

	public EfUserRoleDao getEfUserRoleDao() {
		return efUserRoleDao;
	}

	public void setEfUserRoleDao(EfUserRoleDao efUserRoleDao) {
		this.efUserRoleDao = efUserRoleDao;
	}

	public void save(EfUser transientInstance) {
		efUserDao.save(transientInstance);
	}

	public void update(EfUser instance) {
		efUserDao.update(instance);
	}

	public void delete(EfUser persistentInstance) {
		efUserDao.delete(persistentInstance);
	}
	
	public void deleteUser(EfUser user) {
		List<EfUserRole> list=efUserRoleDao.findUserRoleByUserId(user.getUserId());
		for(EfUserRole userRole:list){
			efUserRoleDao.delete(userRole);
		}
		efUserDao.delete(user);
	}

	public List<EfUser> findByCondition(EFUserModel model) {
		return efUserDao.findByCondition(model);
	}

	public EfUser findById(String userId) {
		return efUserDao.findById(userId);
	}

	public EfUser findByUserAccount(String userAccount) {
		return efUserDao.findByUserAccount(userAccount);
	}
	//修改用户密码
	public void updatePW(String userId,String newPW){
		efUserDao.updatePW(userId, newPW);
	}
	
	/**
	 * 用户登录，用于给别的系统调用
	 * */
	public List loginAuthentication(String userId,String password,String ip){
		List list =new ArrayList();
		EfUser efUser = new EfUser();
		efUser.setUserAccount(userId);
			//密码要MD5
		efUser.setUserPassword(Security.encrypt(password));
		efUser = efUserService.authentication(efUser);
		
		if (efUser == null){
			if(log.isErrorEnabled()) log.error("error.LogonModel.userIdAndPassword.notFound");
			ServletActionContext.getRequest().setAttribute(EfActionConstant.GLOBAL_ERRMSG_TOPAGE,"error.LogonModel.userIdAndPassword.notFound");
			
			list.add("reLogon");
		}else{
			//第三步：获取最顶层(模块)菜单
			List<EfMenu> topMenuList = efMenuService.findTopLevelMenu(userId);
			list.add(topMenuList);
		}
		
		return list;
	}
	
	//进入模块首页
	public List doEnterModel(String userId,String modelId) throws Exception {
		
		List list=new ArrayList();
		// 第二步：获取最顶层(模块)菜单
		List<EfMenu> topMenuList = efMenuService.findSecondLevelMenu(userId, modelId);
		list.add(topMenuList);
		
		// 第三步：获取模块下属菜单
		List<EfMenu> thirdMenuList = efMenuService.findThirdLevelMenu(userId, modelId);
		list.add(thirdMenuList);
		
		//第四步：获取快捷菜单
		List<EfMenu> menuShortcutList = efMenuShortcutService.findByCondition(userId);
		list.add(menuShortcutList);
		
		//系统公告
		List<SystemMessage> systemMessages=systemMessageService.findValidSystemMessage();
		list.add(systemMessages);
		
		return list;
	}

	/**
	 * 写日志
	 */
	public void writeUserLogonLog(String msg,String userId,String ip){
		
		EfUserLoginLog log = new EfUserLoginLog();
		log.setLogId(IdentityGenerator.getInstance().getId());
		log.setLogonDatetime(new Date());
		log.setLogonIp(ip);
		log.setRemark(msg);
		EfUser efUser = new EfUser();
		efUser.setUserId(userId);
		log.setEfUser(efUser);
		
		efUserLoginLogService.save(log);
	}
	
	public EfUserService getEfUserService() {
		return efUserService;
	}

	public void setEfUserService(EfUserService efUserService) {
		this.efUserService = efUserService;
	}

	public EfUserLoginLogService getEfUserLoginLogService() {
		return efUserLoginLogService;
	}

	public void setEfUserLoginLogService(EfUserLoginLogService efUserLoginLogService) {
		this.efUserLoginLogService = efUserLoginLogService;
	}

	public EfMenuService getEfMenuService() {
		return efMenuService;
	}

	public void setEfMenuService(EfMenuService efMenuService) {
		this.efMenuService = efMenuService;
	}

	public EfMenuShortcutService getEfMenuShortcutService() {
		return efMenuShortcutService;
	}

	public void setEfMenuShortcutService(EfMenuShortcutService efMenuShortcutService) {
		this.efMenuShortcutService = efMenuShortcutService;
	}

	public SystemMessageService getSystemMessageService() {
		return systemMessageService;
	}

	public void setSystemMessageService(SystemMessageService systemMessageService) {
		this.systemMessageService = systemMessageService;
	}
}
